<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/10/26
 * Time: 11:40
 */

namespace zhtfrontend\modules\sitepadapi\controllers;

use common\components\helpers\Fun;
use zhtfrontend\models\datas\Arrangement;
use zhtfrontend\models\datas\ClassroomAttr;
use zhtfrontend\models\datas\Course;
use zhtfrontend\models\datas\Goods;
use zhtfrontend\models\datas\GoodsClassTime;
use zhtfrontend\models\datas\OrdercourseHistory;
use zhtfrontend\models\datas\OrderSales;
use zhtfrontend\models\datas\Sales;
use zhtfrontend\modules\sitepadapi\components\rest\ActivebackController;
use yii\data\ArrayDataProvider;
use yii\web\NotFoundHttpException;
use yii\web\Response;

class CourseController extends ActivebackController
{

    public $modelClass = 'zhtfrontend\models\datas\Goods';

    public $serializer = [
        'class' => 'zhtfrontend\modules\sitepadapi\components\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view']);
        return $actions;
    }

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }

    public function actionIndex()
    {
        $data = '';
        $modelClass = $this->modelClass;
        $query = $modelClass::find()->where(['type' => 0, 'site_id' => Fun::getSiteId()])->all();
        foreach ($query as $k => $v) {
            $data[] = [
                'image' => '',
                'course_name' => $v->title,
                'course_modality' => isset($v->course) ? $this->getModality($v->course->teache_form) : '',
                'price' => $v->arrangeCourse->price
            ];
        }
        return new ArrayDataProvider([
            'allModels' => $data,
            'pagination' => [
                'pageSize' => 20
            ],
        ]);
    }

    public function actionView($sales_id)
    {
        return $this->findModel($sales_id);
    }

    public function actionFindbytmc($time = null, $modality = null, $category_id = null)
    {
        $data = [];
        $site_id = Fun::getSiteId();
        $modelClass = $this->modelClass;
        $query = $modelClass::find()->where([
            'type_id' => 1, 'site_id' => $site_id, 'is_shelve' => 1, 'is_delete' => 0, 'is_buy' => 1
        ]);
        if ($time != null) {
            $currentTime = $this->getTime($time);
            $goodsClassTime = GoodsClassTime::find()->select('goods_id')
                ->where(['class_day' => $currentTime['class_day']])
                ->andWhere(['between', 'start_time', $currentTime['start_time'], $currentTime['end_time']])
                ->column();
            $query = $query->andWhere(['in', 'id', $goodsClassTime]);
        }
        if ($modality != null) {
            $course_id = Course::find()->where(['teache_form' => $modality])->asArray()->column();
            $arrangement = Arrangement::find()->select('id')->where(['in', 'course_id', $course_id])->asArray()->column();
            $query = $query->andWhere(['in', 'arrangement_id', $arrangement]);
        }
        if ($category_id != null) {
            $query = $query->andWhere(['cat_id' => $category_id]);
        }
        $salesData = $query->all();
        if (!empty($salesData)) {
            foreach ($salesData as $k => $v) {
                $data[] = [
                    'sales_id' => $v->id,
                    'image' => $v->picture,
                    'course_name' => $v->name,
                    'teacher' => '教师',
                    'price' => $v->price
                ];
            }
        } else {
            $data = [];
        }
        $provider = new ArrayDataProvider([
            'allModels' => $data,
            'pagination' => ['pageSize' => 21]
        ]);
        return $provider;
    }


    protected function getSales($data)
    {
        return [
            'sales_id' => $data->id,
            'image' => isset($data->teacher) ? $data->teacher->img : '',
            'course_name' => $data->title,
            'teacher' => isset($data->teacher) ? $data->teacher->name : '',
            'price' => $data->price
        ];
    }

    public function actionFindbysid($student_id)
    {
        $data = '';
        $order_sales_id = OrdercourseHistory::find()->select('order_sales_id')->where(['bind_id' => $student_id])->asArray()->column();
        $sales_id = OrderSales::find()->select('sales_id')->where(['in', 'id', $order_sales_id])->andWhere(['type' => 0])->asArray()->column();
        $modelClass = $this->modelClass;
        $query = $modelClass::find()->where(['in', 'id', $sales_id])->all();
        foreach ($query as $k => $v) {
            $data[] = [
                'image' => '',
                'course_name' => $v->title,
                'course_modality' => isset($v->course) ? $this->getModality($v->course->teache_form) : '',
                'price' => $v->arrangeCourse->price
            ];
        }
        return new ArrayDataProvider([
            'allModels' => $data,
            'pagination' => [
                'pageSize' => 20
            ],
        ]);
    }

    public function actionFindbyday($day)
    {
        $data = '';
        $modelClass = $this->modelClass;
        $classroomAttr = ClassroomAttr::find()->select('id')->where([
            'classday' => $day
        ])->column();
        $query = $modelClass::find()->where([
            'site_id' => Fun::getSiteId(), 'type' => 0
        ])->andWhere(['in', 'classroom_attr_id', $classroomAttr])->all();
        foreach ($query as $k => $v) {
            $data[] = [
                'image' => '',
                'course_name' => $v->title,
                'course_modality' => isset($v->course) ? $this->getModality($v->course->teache_form) : '',
                'price' => $v->arrangeCourse->price
            ];
        }
        return new ArrayDataProvider([
            'allModels' => $data,
            'pagination' => [
                'pageSize' => 20
            ],
        ]);
    }


    protected function findModel($sales_id)
    {
        //$model = Sales::findOne(['id' => $sales_id]);
        $model = Goods::findOne(['id' => $sales_id]);
        if (empty($model)) {
            try {
                throw new NotFoundHttpException('信息不存在');
            } catch (NotFoundHttpException $e) {
                return ['status' => 0, 'message' => 'error', 'result' => $e->getMessage()];
            }
        }
        $data['items'] = [
            'user_id' => '42',
            'image' => [$model->picture],
            'course_name' => $model->name,
            'collect_status' => '0',
            //'course_modality' => isset($model->goodsattr) ? $this->getModality($model->goodsattr->teacher_form) : '',
            'course_modality'=>$this->getModality(1),
            /*'course_time' => isset($model->goodsClassTime) ?
                $this->getDay($model->goodsClassTime->class_day) . ' ' .
                $this->getSe($model->goodsClassTime->start_time) . '-' .
                $this->getSe($model->goodsClassTime->end_time) : '',*/
            'course_time'=>'9:00-10:00',
            //'teacher_name' => isset($model->goodsattr) ? $model->teacher->name : '',
            'teacher_name' => 'name',
            'like' => '66',
            'mechanic' => isset($model->mechanic) ? $model->mechanic->name : '',
            'teacher_introduction' => '2006年以优异的成绩考入西安音乐学院管弦系师从冯继铖教授学习巴松演奏。
                                       2009年参加“中国首届国际巴松艺术节”并代表学校在全国音乐院校综合交流音乐会上演奏了《胡梅尔F大调协奏曲》《贝格特巴松三重奏》。
                                       木管五重奏组入围了2009年“中国金钟奖室内乐重奏比赛决赛”。
                                       2010年毕业后考入四川爱乐乐团工作.现任四川爱乐乐团首席巴松演奏员。',
            'ability' => '',
            'time' => ['10:10-11:00', '8:00-9:00'],
            'course_details' => $model->description
        ];
        return $data;
    }

    protected function getTime($time)
    {
        $classDay = substr($time, 0, stripos($time, '/'));
        $start = substr($time, stripos($time, '/') + 1, stripos($time, '-') - 2);
        $startTime = substr($start, 0, stripos($start, ':')) . substr($start, stripos($start, ':') + 1);
        $end = substr($time, stripos($time, '-') + 1);
        $endTime = substr($end, 0, stripos($end, ':')) . substr($end, stripos($end, ':') + 1);
        return [
            'class_day' => $classDay,
            'start_time' => $startTime,
            'end_time' => $endTime
        ];
    }

    protected function getModality($modality)
    {
        switch ($modality) {
            case 0:
                return '一对一';
                break;
            case 1:
                return '小班制';
                break;
            case 2:
                return '普通班';
                break;
        }
        return '';
    }

    protected function getDay($day)
    {
        switch ($day) {
            case 1:
                return '周一';
                break;
            case 2:
                return '周二';
                break;
            case 3:
                return '周三';
                break;
            case 4:
                return '周四';
                break;
            case 5:
                return '周五';
                break;
            case 6:
                return '周六';
                break;
            case 7:
                return '周日';
                break;
        }
        return '';
    }

    protected function getSe($time)
    {
        $currentTime = strlen($time) == 3 ? substr($time, 0, 1) . ':' . substr($time, 1) : substr($time, 0, 2) . ':' . substr($time, 2);
        return $currentTime;
    }

    protected function getOrderTime($data)
    {
        $timeArr = [];

        $salesData = Sales::find()->where(['course_id' => $data->course_id, 'teacher_id' => $data->teacher_id])->all();
        foreach ($salesData as $k => $v) {
            $day = isset($v->classroomAttr) ? $this->getDay($v->classroomAttr->classday) : '';
            $startTime = isset($v->classroomAttr) ? $this->getSe($v->classroomAttr->start_time) : '';
            $endTime = isset($v->classroomAttr) ? $this->getSe($v->classroomAttr->end_time) : '';
            $timeArr[] = $day . ' ' . $startTime . '-' . $endTime;
        }
        return $timeArr;
    }


}